var ds;
function displaySubTypes() {
	Ext.form.Field.prototype.msgTarget = 'side';
	Ext.QuickTips.init();

	var sm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	ds = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : DISPATCH_URL
		}),
		reader : new Ext.data.JsonReader({
			root : 'subTypes',
			id : 'id',
			fields : [ {
				name : 'id'
			}, {
				name : 'name'
			}, {
				name : 'description'
			}, {
				name : 'parent'
			} ]
		})
	});
	var gridCm = new Ext.grid.ColumnModel([ new Ext.grid.RowNumberer(), {
		header : TXT_sub_type_name,
		dataIndex : 'name',
		width : 200
	}, {
		header : TXT_sub_type_description,
		dataIndex : 'description',
		width : 200
	}, {
		header : TXT_sub_type_parent,
		dataIndex : 'parent',
		width : 200
	} ]);
	var btnCreate = new Ext.Button({
		text : TXT_common_create,
		tooltip : TXT_common_create,
		iconCls : 'icoAdd'
	});

	var btnEdit = new Ext.Button({
		text : TXT_common_edit,
		tooltip : TXT_common_edit,
		iconCls : 'icoDetail'
	});

	var btnDelete = new Ext.Button({
		text : TXT_common_delete,
		tooltip : TXT_common_delete,
		iconCls : 'icoDel'
	});
	btnCreate.on('click', function() {
		createSubTypeForm('', '新增子类型');
	});
	btnEdit.on('click', function() {
		if (sm.getCount() == 0) {
			Ext.MessageBox.alert(TXT_common_hint, TXT_user_need);
			return;
		}
		var record = sm.getSelected();
		createSubTypeForm(record.id, '修改子类型');
	});
	btnDelete
			.on(
					'click',
					function() {
						if (sm.getCount() == 0) {
							Ext.MessageBox
									.alert(TXT_common_hint, TXT_user_need);
							return;
						}
						var record = sm.getSelected();
						Ext.MessageBox
								.confirm(
										TXT_common_hint,
										TXT_user_delete_confirmation,
										function(btn) {
											if (btn == 'yes') {
												BestvSearch.showWaitingWin();
												Ext.Ajax
														.request({
															url : DISPATCH_URL,
															method : 'POST',
															params : 'cmd=subType&action=delete&id='
																	+ record.id,
															success : function(
																	response,
																	options) {
																Ext.MessageBox
																		.hide();
																var s = response.responseText;
																if (s == '[error]') {
																	Ext.MessageBox
																			.alert(
																					TXT_common_hint,
																					TXT_common_access_fail);
																} else if (s == '[null]') {
																	Ext.MessageBox
																			.alert(
																					TXT_common_hint,
																					TXT_common_relogin,
																					function() {
																						window.location.href = LOGIN_URL;
																					});
																} else {
																	var result = Ext.util.JSON
																			.decode(s);
																	if (result.result == 'success') {
																		Ext.MessageBox
																				.alert(
																						TXT_common_hint,
																						TXT_common_delete_success);
																		reloadSubTypes();
																	} else if (result.message == 'noright') {
																		Ext.MessageBox
																				.alert(
																						TXT_common_hint,
																						TXT_delete_subtype_no_right);
																		return;
																	}
																}
															},
															failure : function(
																	response,
																	options) {
																Ext.MessageBox
																		.hide();
																Ext.MessageBox
																		.alert(
																				TXT_common_hint,
																				TXT_common_access_fail);
															}
														});
											}
										});
					});
	var grid = new Ext.grid.GridPanel({
		el : 'grid',
		ds : ds,
		cm : gridCm,
		sm : sm,
		stripeRows : true,
		loadMask : true,
		title : TXT_sub_type_title,
		tbar : [ btnCreate, '-', btnEdit, '-', btnDelete ]
	});
	ds.baseParams = {
		cmd : 'subType',
		action : 'find'
	};
	ds.on('load', function() {
		grid.getSelectionModel().selectFirstRow();
	});
	ds.load();

	BestvSearch.glb_mainPanel.add(grid);
	BestvSearch.drawMainFrame(TXT_app_name + ' - ' + TXT_sub_type_title);
}

function createSubTypeForm(subTypeId, s_title) {
	var tid = subTypeId;
	var parentDs = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : DISPATCH_URL
		}),
		reader : new Ext.data.JsonReader({
			root : 'types',
			id : 'id',
			fields : [ {
				name : 'id'
			}, {
				name : 'name'
			}, {
				name : 'description'
			} ]
		})
	});
	parentDs.baseParams = {
		'cmd' : 'type',
		'action' : 'find'
	};
	parentDs.on('loadexception', function() {
	});
	var parentCombo = new Ext.form.ComboBox({
		// tpl: '<tpl for="."><div ext:qtip="{bic}. {name}"
		// class="x-combo-list-item">{bic}</div></tpl>',
		fieldLabel : TXT_parent_type,
		id : 'parentType',
		name : 'parentType',
		displayField : 'name',
		emptyText : '选择父类...',
		valueField : 'id',
		typeAhead : true,
		triggerAction : 'all',
		selectOnFocus : true,
		triggerClass : 'x-form-trigger2',
		store : parentDs,
		lazyInit : false,
		editable : false,
		allowBlank : false
	});
	parentDs.load();
	var subTypeForm = new Ext.FormPanel({
		labelAlign : 'right',
		labelWidth : 80,
		height : 160,
		width : 330,
		margins : '0 0 2 0',
		cmargins : '0 0 2 0',
		frame : true,
		defaults : {
			width : 180
		},
		defaultType : 'textfield',
		items : [ {
			fieldLabel : TXT_type_name,
			id : 'typeName',
			name : 'typeName',
			allowBlank : false
		}, {
			fieldLabel : TXT_type_description,
			id : 'typeDescription',
			name : 'typeDescription',
			allowBlank : false
		}, parentCombo ]
	});
	var win = null;
	if (!win) {
		win = new Ext.Window(
				{
					title : s_title,
					height : 160,
					width : 330,
					autoScroll : false,
					layout : 'form',
					border : false,
					minimizable : false,
					maximizable : false,
					resizable : false,
					modal : false,
					layoutConfig : {
						animate : false
					},
					items : subTypeForm,
					buttonAlign : 'center',
					buttons : [
							{
								text : TXT_common_OK,
								handler : function() {
									if (!subTypeForm.form.isValid())
										return;
									var typeName = subTypeForm.findById(
											'typeName').getValue();
									var typeDescription = subTypeForm.findById(
											'typeDescription').getValue();
									var parentId = subTypeForm.findById(
											'parentType').getValue();
									if (typeName == '') {
										Ext.MessageBox.alert(TXT_common_hint,
												TXT_type_name_not_null);
										return;
									}
									if (parentId == '') {
										Ext.MessageBox.alert(TXT_common_hint,
												TXT_parent_type_not_null);
										return;
									}
									BestvSearch.showWaitingWin();
									Ext.Ajax
											.request({
												url : DISPATCH_URL,
												mehtod : 'post',
												params : 'cmd=subType&action=save&id='
														+ tid
														+ '&typeName='
														+ typeName
														+ '&typeDescription='
														+ typeDescription
														+ '&parentid='
														+ parentId,
												success : function(response,
														options) {
													Ext.MessageBox.hide();
													var s = response.responseText;
													if (s == 'error') {
														Ext.MessageBox
																.alert(
																		TXT_common_hint,
																		TXT_common_access_fail);
													} else if (s == 'null') {
														Ext.MessageBox
																.alert(
																		TXT_common_hint,
																		TXT_common_relogin,
																		function() {
																			window.location.href = '../eni/common/logout.jsp';
																		});
													} else {
														var result = Ext.util.JSON
																.decode(s);
														if (result.result == 'success') {
															// if(result.message
															// == 'new')
															// Ext.MessageBox.alert(TXT_common_hint,TXT_site_create_success,function(){win.close();});
															// else
															// if(result.message
															// == 'edit')
															// Ext.MessageBox.alert(TXT_common_hint,TXT_edit_success,function(){win.close();});
															win.close();
															reloadSubTypes();
														} else if (result.message == 'name') {
															Ext.MessageBox
																	.alert(
																			TXT_common_hint,
																			TXT_sub_type_name_exist);
														}
													}
												},
												failure : function(respond,
														options) {
													Ext.MessageBox.hide();
													Ext.MessageBox
															.alert(
																	TXT_common_hint,
																	TXT_common_access_fail);
												}
											});
								}
							}, {
								text : TXT_common_cancel,
								handler : function() {
									win.close();
								}
							} ]
				});
	}
	win.on('show', function() {
		if (tid != '') {
			Ext.Ajax.request({
				url : DISPATCH_URL,
				method : 'post',
				params : 'cmd=subType&action=get&id=' + tid,
				success : function(response, options) {
					var s = response.responseText;
					if (s == '[error]')
						Ext.MessageBox.alert(TXT_common_hint,
								TXT_common_access_fail);
					else if (s == '[null]') {
						Ext.MessageBox.alert(TXT_common_hint,
								TXT_common_relogin, function() {
									window.location.href = LOGIN_URL;
								});
					} else {
						var result = Ext.util.JSON.decode(s);
						subTypeForm.findById('typeName').setValue(result.name);
						subTypeForm.findById('typeDescription').setValue(
								result.description);
						subTypeForm.findById("parentType").setValue(
								result.parentId);
						// var parentRecords = parentDs.reader.jsonData;
						// for(i = 0; i < parentRecords.types.length; i++)
						// {
						// var t_type = parentRecords.types[i];
						// if( t_type.name == result.parent)
						// {
						// subTypeForm.findById('parentType').setValue(t_type.id);
						// }
						// }
					}
				},
				failure : function(response, options) {
					Ext.MessageBox.alert(TXT_common_hint,
							TXT_common_access_fail);
				}
			});
		}
	});
	win.show();
}
function reloadSubTypes() {
	ds.reload();
}

Ext.onReady(displaySubTypes);